/*
 * Copyright (c) 2017. danlu.com Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.activity.delivery.order.delegate;

import java.util.List;

import cn.com.dhc.danlu.activity.delivery.order.decorate.Visitable;
import cn.com.dhc.danlu.activity.delivery.order.utill.Modle2BeanUtil;
import cn.com.dhc.danlu.activity.delivery.order.utill.VisitableWrapper;
import cn.com.dhc.danlu.delegate.delegate.list.BaseListDataDelegate;
import cn.com.dhc.danlu.dlware.OperatorUtils;
import cn.com.hd.mall.web.webservices.entity.BaseResponse;
import cn.com.hd.mall.web.webservices.entity.request.order.OrderListRequest;
import cn.com.hd.mall.web.webservices.entity.response.order.OrderListResponse;
import rx.Observable;
import rx.functions.Func1;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * author: wuhaiyang(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-02-14 下午6:46<br/>
 * <p>
 * 订单列表
 * </p>
 */
public class OrderListDelegate extends BaseListDataDelegate<IOrderListCallback> {
    private String statusCode;

    public OrderListDelegate(String statusCode) {
        this.statusCode = statusCode;
    }

    @Override
    protected Observable createRequestObservable(int page) {
        // Request
        OrderListRequest request = new OrderListRequest();
        // 设置token
        request.setToken(agent.getToken());
        // 设置订单状态
        request.setOrderStatus(statusCode);
        // 设置页
        request.setPage(page);
        // 设置行
        request.setRows(10);
        Observable<VisitableWrapper<Visitable>> observable = agent.getApi().getTerminalOrderList(request)
                .compose(OperatorUtils.<OrderListResponse>proccessError())
                .map(new Func1<OrderListResponse, VisitableWrapper<Visitable>>() {
                    @Override
                    public VisitableWrapper<Visitable> call(OrderListResponse res) {
                        VisitableWrapper<Visitable> wrapper = new VisitableWrapper<>();
                        wrapper.setSuccess(res.getSuccess());
                        wrapper.setDatas(Modle2BeanUtil.convert(res));
                        return wrapper;
                    }
                });
        return OperatorUtils.convertThread(observable);
    }

    @Override
    protected List excavateList(BaseResponse model) {
        VisitableWrapper wrapper = (VisitableWrapper) model;
        return wrapper.getDatas();
    }
}
